// import 'dart:async';
// import 'dart:io';
//
// import 'package:flutter/material.dart';
// import 'package:in_app_purchase/in_app_purchase.dart';
// import 'package:in_app_purchase_android/billing_client_wrappers.dart';
// import 'package:in_app_purchase_android/in_app_purchase_android.dart';
// import 'package:in_app_purchase_storekit/in_app_purchase_storekit.dart';
// import 'package:in_app_purchase_storekit/store_kit_wrappers.dart';
//
// class BuyInApp {
//   static late StreamSubscription<List<PurchaseDetails>> _subscription;
//   static late InAppPurchase _inAppPurchase;
//   static const List<String> _kProductIds = <String>[
//     'com.lltecbj.geomancy.year',
//   ];
//
//   static init() {
//     _inAppPurchase = InAppPurchase.instance;
//
//     //监听购买事件
//     final Stream<List<PurchaseDetails>> purchaseUpdated = _inAppPurchase.purchaseStream;
//     _subscription = purchaseUpdated.listen((purchaseDetailsList) {
//       _listenToPurchaseUpdated(purchaseDetailsList);
//     }, onDone: () {
//       _subscription.cancel();
//     }, onError: (error) {
//       print('error==$error');
//       print('购买失败');
//     });
//   }
//   ///内部购买更新监听
//   static Future _listenToPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) async {
//
//   }
//
//   static void buyProduct(String productId) async {
//
//   }
//
//
//
// }



import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:flutter/material.dart';

class BuyInApp {
  // 单例模式
  static final BuyInApp _instance = BuyInApp._internal();

  factory BuyInApp() {
    return _instance;
  }

  BuyInApp._internal();

  // 初始化 In-App Purchase
  Future<void> initialize() async {
    final bool available = await InAppPurchase.instance.isAvailable();
    if (!available) {
      print('In-App Purchase not available on this device.');
      return;
    }
  }

  // 获取所有可购买的商品
  Future<List<ProductDetails>> getProducts(List<String> productIds) async {
    final ProductDetailsResponse response = await InAppPurchase.instance.queryProductDetails(productIds.toSet());
    if (response.notFoundIDs.isNotEmpty) {
      print('Products not found: ${response.notFoundIDs}');
    }
    return response.productDetails;
  }

  // 发起购买
  Future<void> buyProduct(ProductDetails product) async {
    final PurchaseParam purchaseParam = PurchaseParam(productDetails: product);
    await InAppPurchase.instance.buyNonConsumable(purchaseParam: purchaseParam);
  }

  // 监听购买更新
  void listenToPurchaseUpdated() {
    InAppPurchase.instance.purchaseStream.listen((List<PurchaseDetails> purchaseDetailsList) {
      _handlePurchaseUpdates(purchaseDetailsList);
    });
  }

  // 处理购买结果
  void _handlePurchaseUpdates(List<PurchaseDetails> purchaseDetailsList) {
    for (var purchaseDetails in purchaseDetailsList) {
      if (purchaseDetails.status == PurchaseStatus.purchased) {
        // 完成购买后处理逻辑
        print('Purchase successful: ${purchaseDetails.productID}');
      } else if (purchaseDetails.status == PurchaseStatus.error) {
        // 处理错误
        print('Purchase error: ${purchaseDetails.error}');
      } else if (purchaseDetails.status == PurchaseStatus.restored) {
        // 恢复购买处理
        print('Purchase restored: ${purchaseDetails.productID}');
      }
      // 处理完购买后，确保确认购买
      if (purchaseDetails.pendingCompletePurchase) {
        InAppPurchase.instance.completePurchase(purchaseDetails);
      }
    }
  }

  // 结束当前购买
  Future<void> completePurchase(PurchaseDetails purchaseDetails) async {
    await InAppPurchase.instance.completePurchase(purchaseDetails);
  }
}
